<?php
/**
 * 用户模块接口
 * User: cqfut
 * Date: 2018/11/1
 * Time: 11:44
 */
//require_once (ROOTDIR."admin/model/csrf_check.php");
require_once ("../../config/path_config.php");
include_once (ROOTDIR."common/base_router.php");
require_once (ROOTDIR."config/sys_config.php");
require_once (ROOTDIR."admin/model/course_schedule_model.php");

class CourseScheduleRouter extends BaseRouter {
    public function __construct() {
        parent::__construct();
    }

    function getScheduleList() {
        $title = $this->getReqVal('title');
        $pageId = $this->getReqVal('pageid');
        $beginIdx = $pageId * SysConfig::$PAGESIZE;
        $model = new CourseScheduleModel();
        // 获取用户的数量
        $cnt = $model->getCourseScheduleCnt($title);
        $maxPageId = floor($cnt/SysConfig::$PAGESIZE);
        // 获取所有用户的用户列表
        $userId = BaseRouter::getSession('user_id');
        $recordList = $model->getCourseScheduleList($title, $userId, $beginIdx, SysConfig::$PAGESIZE);

        $this->setRspVal('record_list', $recordList);
        $this->setRspVal('max_pageid', $maxPageId);
        $this->setRspVal('page_size', SysConfig::$PAGESIZE);
    }

    function getScheduleDetail() {
        $userId = BaseRouter::getSession('user_id');
        if ( $userId == null ) {
            $this->setRspStatus(0, -1,"用户未登录" );
            return;
        }
        $id = $this->getReqVal('id');
        if ( $id == null ) {
            $this->setRspStatus(0, -2,"参数错误" );
            return;
        }

        $model = new CourseScheduleModel();
        $recordDetail = $model->getCourseScheduleDetail($id, $userId);
        $this->setRspData($recordDetail);
    }

    function addSchedule() {
        $createrId = BaseRouter::getSession('user_id');
        if ( $createrId == null ) {
            $this->setRspStatus(0, -1,"用户未登录" );
            return;
        }

        if ( BaseRouter::getSession('administrator') == null
            && BaseRouter::getSession('jgadmin') == null ) {
            $this->setRspStatus(0, -3,"无操作权限" );
            return;
        }

        $args = array();
        array_push($args, 'title', 'content');
        // 必要参数判断
        if ( ! $this->checkParam($args) ) {
            $this->setRspStatus(0, -2,"缺少必要字段" );
            return;
        }

        $title = $this->getReqVal('title');
        // 无需url解码,php会自动进行url解码
        $content = $this->getReqVal('content');

        $model = new CourseScheduleModel();
        $ret = $model->addCourseSchedule($title, $content, $createrId);
        if ( $ret >= 0 ) {
            $this->setRspStatus(1, 0,"添加文本成功" );
        }
        else {
            $this->setRspStatus(0, -1,"数据库错误" );
        }
    }

    function editSchedule() {
        $args = array();
        array_push($args, 'id', 'title', 'content');
        // 必要参数判断
        if ( ! $this->checkParam($args) ) {
            $this->setRspStatus(0, -2,"缺少必要字段" );
            return;
        }

        $modifyerId = BaseRouter::getSession('user_id');
        if ( $modifyerId == null ) {
            $this->setRspStatus(0, -1,"用户未登录" );
            return;
        }

        if ( BaseRouter::getSession('administrator') == null
            && BaseRouter::getSession('jgadmin') == null ) {
            $this->setRspStatus(0, -3,"无操作权限" );
            return;
        }

        $id = $this->getReqVal('id');
        $title = $this->getReqVal('title');
        $content = $this->getReqVal('content');
        $model = new CourseScheduleModel();
        $ret = $model->editCourseSchedule($id, $title, $content, $modifyerId);
        if ( $ret > 0 ) {
            $this->setRspStatus(1, 0,"修改成功" );
        }
        else {
            $this->setRspStatus(0, -1,"数据库错误" );
        }
    }

    function delSchedule() {
        $args = array();
        array_push($args, 'id');
        // 必要参数判断
        if ( ! $this->checkParam($args) ) {
            $this->setRspStatus(0, -2,"缺少必要字段" );
            return;
        }
        $id = $this->getReqVal('id');

        // 判断是否具有删除除管理员以外所有用户的权限
        if ( BaseRouter::getSession('jgadmin') == null
            && BaseRouter::getSession('administrator') == null ) {
            $this->setRspStatus(0, -3,"不具有操作权限" );
        }

        $createrId = BaseRouter::getSession('user_id');

        $model = new CourseScheduleModel();
        $ret = $model->delCourseSchedule($id, $createrId);
        if ( $ret > 0 ) {
            $this->setRspStatus(1, 0,"删除文本信息成功" );
        }
        else {
            $this->setRspStatus(0, -1,"删除文本信息失败" );
        }
    }

    public function checkPrivileges() {
        $priviNames = $this->getReqVal('privi_names');
        $heckRes = array();
        foreach ( $priviNames as $pname ) {
            $checkCode = BaseRouter::getSession($pname) != null;
            $heckRes[$pname] = $checkCode;
        }
        $this->setRspData($heckRes);
    }

    public function run() {
        $op = $this->getReqVal('op');
        if ( $op == 'add' ) {
            $this->addSchedule();
        }
        else if ( $op == 'del' ) {
            $this->delSchedule();
        }
        else if ( $op == 'edit' ) {
            $this->editSchedule();
        }
        else if ( $op == 'search' ) {
            $this->getScheduleList();
        }
        else if ( $op == 'fetch' ) {
            $this->getScheduleDetail();
        }
        else {
            $this->setRspStatus(0, -1,"参数错误" );
        }
    }
}

$router = new CourseScheduleRouter();
$router->init();
$router->run();
$router->finish();